package com.example.healthsystem.controller;


import com.example.healthsystem.entity.*;
import com.example.healthsystem.mapper.UserMapper;
import com.example.healthsystem.model.dto.ProfileInfo;
import com.example.healthsystem.service.HealthregService;
import com.example.healthsystem.service.ReglistService;
import com.example.healthsystem.service.UserService;
import com.example.healthsystem.util.StringUtils;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;

@RequestMapping("/admin")
//@RestController
@Controller // --- remember: value of return is **.html
@Api(value="管理员接口",tags="管理员")
public class AdminController {
    @Autowired
    UserService userService;
    @Autowired
    ReglistService reglistService;
    @Autowired
    HealthregService healthregService;

    private UserMapper userMapper;

    // ---------------------------------------------------------------------------------
    // --------------------------------- 以下进行页面跳转 ---------------------------------
    // ---------------------------------------------------------------------------------
    /**
     * 跳转到 用户注册页面
     * @return
     */
    @RequestMapping("/addUser")
    public String addUser() {
        return "/admin/user/register";
    }

    /**
     * 跳转到 健康登记信息汇总 页面
     * @return
     */
    @RequestMapping("/healthreg")
    public String healthreg(){
        return "/admin/healthreg/healthreg";
    }

    /**
     * 跳转到 未登记健康表信息 页面
     * @return
     */
    @RequestMapping("/reglist")
    public String reglist(){
        return "/admin/reglist/reglist";
    }

    /**
     * 跳转到 用户总列表页面
     * @return
     */
    @RequestMapping("/user")
    public String user(){
        return "admin/user/user";
    }

    @RequestMapping("/change_msg")
    public String change_msg(Model model,String username){
        model.addAttribute("username",username);
        return "admin/user/change_msg";
    }

    // ---------------------------------------------------------------------------------
    // -------------------------------- 【管理员】进行操作 --------------------------------
    // ---------------------------------------------------------------------------------

    // --- 【返回页面】 ---
    // ==================

    /**
     * insert\post
     * 管理员注册用户
     * @param id
     * @param username
     * @param password
     * @param name
     * @param college
     * @param role
     * @param situ
     * @return
     */
    @RequestMapping("/register")
    @ResponseBody
//    @ApiImplicitParam(name="user",value = "新增用户数")
//    @ApiOperation(value = "用户注册",notes="添加用户")
    public String registerUser(String id,String username,
                               String password,String name,
                               String college,String role,
                               int situ){
        user user=new user();

        user.setId(id);
        user.setUsername(username);
        user.setPassword(password);
        user.setName(name);
        user.setCollege(college);
        user.setRole(role);
        user.setSitu(situ);

        String haveId = userService.haveId(username);

        if("true".equals(haveId)){
            return "false";
        }else{
            userService.registerUser(user);
        }
        return "true";
    }

    /**
     * insert\post
     * 管理员插入未登记表数据
     * @param reglist
     * @return
     */
    @PostMapping("/insertList")
    public String insertList(@RequestBody reglist reglist){
        if(StringUtils.isEmpty(reglist.getUser_id(),
                reglist.getCollege(),
                reglist.getName(),
                String.valueOf(reglist.getUnregnum()),
                String.valueOf(reglist.getLastregtime()
                ))){
            return null;
        }
        reglistService.insertList(reglist);
        return "/admin/reglist/reglist";
    }

    /**
     * delete\delete
     * 管理员 删除用户
     * @param name
     * @return
     */
    @DeleteMapping("/deleteUser")
    public String deleteUser(@RequestParam String name){
        userService.deleteUser(name);
        return "/admin/user/user";
    }

    /**
     * delete\delete
     * 管理员 删除未登记表 中的数据
     * @param name
     * @return
     */
    @DeleteMapping("/deleteList")
    public String deleteList(@RequestParam String name){
        reglistService.deleteList(name);
        return "/admin/register";
    }

    /**
     * upgrade\post
     * 管理员 修改 用户/管理员 信息
     * @param user
     * @return
     */
    @RequestMapping("/changeUserMsg")
    public String changeUserMsg(user user){
        if(user.getPassword()!=null&&!"".equals(user.getPassword())){
            return "redirect:/";
        }
        userService.adminchangeProfile(user);
        return "redirect:/admin/user/user";
    }
    // ------------------------------以上三个可能需要进行调整

    /**
     * select\get
     * 根据搜索人的姓名来查找健康未登记表
     * @param model
     * @param name
     * @return
     */
    @GetMapping("/searchNameList")
    public String searchNameList(Model model, @RequestParam String name){
        reglist reglist= reglistService.searchNameList(name);
        model.addAttribute("reglist",reglist);
        return "/admin/reglist/reglist_msg";
    }

    /**
     * 搜索健康登记表 依据 姓名和登记时间搜索
     * @param model
     * @param name
     * @return
     */
    @RequestMapping("/searchHealthName")
    public String searchNameList(Model model,
                                 @RequestParam String name,
                                 @RequestParam Timestamp time){
        healthreg healthregs= healthregService.searchHealthDate(name,time);
        model.addAttribute("healthreg",healthregs);
        return "/admin/healthreg/healthreg_msg";
    }

    // --- 【返回响应数据】 ---
    // ==================

    /**
     * 搜索所有的健康登记表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/searchAllList")
    @ResponseBody
    public PageInfo<reglist> searchAllList(@RequestParam(defaultValue = "1") int pageNum,
                                           @RequestParam(defaultValue = "10") int pageSize){
        List<reglist> reglists=reglistService.searchAllList(pageNum, pageSize);
        return new PageInfo<>(reglists);
    }

    /**
     * 搜索所有的用户
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/findAlluser")
    @ResponseBody
    public PageInfo<user> findAlluser(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize){
        List<user> userList=userService.findAlluser(pageNum, pageSize);
        return new PageInfo<>(userList);
    }

    /**
     * 搜索所有的健康登记表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/searchHealth")
    @ResponseBody
    public PageInfo<healthreg> searchHealth(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize){
        List<healthreg> healthreglist= healthregService.searchHealth(pageNum, pageSize);
        return new PageInfo<>(healthreglist);
    }
}
